package com.android.panballgame;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.media.MediaPlayer;
import android.content.Context;

public class PBMusic extends Service {
	public static boolean isRunning = false;//서비스가 실행중인지 아닌지 판단하는 변수
	static MediaPlayer player;
	@Override
	public IBinder onBind(Intent arg0){
		return null;
	}
	@Override
	public void onCreate(){
		super.onCreate();
		setMusicOptions(this, PBEngine.LOOP_BACKGROUND_MUSIC, PBEngine.R_VOLUME, 
				PBEngine.L_VOLUME, PBEngine.MUSIC);
	}
	public void setMusicOptions(Context context, boolean isLooped, int rVolume, int lVolume, int soundFile) {
		player = MediaPlayer.create(context, soundFile);
		player.setLooping(isLooped);
		player.setVolume(rVolume, lVolume);
	}
	public int onStartCommand(Intent intent, int flags, int startId){
		try
		{
			player.start();
			isRunning = true;
		}catch(Exception e){
			isRunning = false;
			player.stop();
		}
		return 1;
	}
	public static void onStart(){
		player.start();
		isRunning = true;
	}
	public void onStop(){
		isRunning = false;
	}
	
	public IBinder onUnBind(Intent arg0){
		//TODO 자동으로 생성된 메서드 일부분
		return null;
	}
	  public boolean isPlaying() {
	        return player.isPlaying();
	    }
	public static void onPause(){    
		player.pause();  
		}
	
	public void onDestory(){
		if(player.isPlaying())
			player.stop();
		player.release();
		player = null;
	}
	@Override
	public void onLowMemory(){
		player.stop();
	}
}
